package com.train.system.facade;

import cn.hutool.core.bean.BeanUtil;
import com.train.common.group.AddGroup;
import com.train.common.group.EditGroup;
import com.train.common.group.QueryGroup;
import com.train.common.resp.CommonResp;
import com.train.common.resp.PageResp;
import com.train.system.dto.station.StationAddOrEditDTO;
import com.train.system.dto.station.StationPageDTO;
import com.train.system.req.station.StationAddOrEditReq;
import com.train.system.req.station.StationPageReq;
import com.train.system.resp.station.StationPageResp;
import com.train.system.service.StationService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("station")
public class StationFacade {

    private final StationService stationService;

    @PostMapping("page")
    public CommonResp pageList(@RequestBody @Validated(QueryGroup.class) StationPageReq req) {
        StationPageDTO stationPageDTO = BeanUtil.copyProperties(req, StationPageDTO.class);
        PageResp<StationPageResp> pageResp = stationService.pageList(stationPageDTO);
        return CommonResp.ok(pageResp);
    }

    @PostMapping("add")
    public CommonResp add(@RequestBody @Validated(AddGroup.class) StationAddOrEditReq req) {
        StationAddOrEditDTO stationAddOrEditDTO = BeanUtil.copyProperties(req, StationAddOrEditDTO.class);
        stationService.addOrEditStation(stationAddOrEditDTO);
        return CommonResp.ok();
    }

    @PostMapping("edit")
    public CommonResp edit(@RequestBody @Validated(EditGroup.class) StationAddOrEditReq req) {
        StationAddOrEditDTO stationAddOrEditDTO = BeanUtil.copyProperties(req, StationAddOrEditDTO.class);
        stationService.addOrEditStation(stationAddOrEditDTO);
        return CommonResp.ok();
    }

    @DeleteMapping("remove/{id}")
    public CommonResp delete(@PathVariable("id") Long id) {
        stationService.remove(id);
        return CommonResp.ok();
    }

    @PostMapping("all")
    public CommonResp allStation() {
        return CommonResp.ok(stationService.allStation());
    }
}
